package com.blb.missiom.service.Impl;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blb.missiom.entity.Permission;
import com.blb.missiom.mapper.PermissionMapper;
import com.blb.missiom.service.IPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xray
 * @since 2023-01-31
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements IPermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    //查询所有权限
    @Cacheable(cacheNames = "Permission" , key = "T(String).valueOf(1)")
    @Override
    public List<Permission> getPermissions() {
        return permissionMapper.selectList(null);
    }
    //分页查询权限
    @Cacheable(cacheNames = "PermissionPage" , key = "T(String).valueOf(#page.current)")
    @Override
    public Page<Permission> getPermissionsPage(Page<Permission> page) {
        return permissionMapper.selectPage(page,null);
    }
    //通过id查询
    @Cacheable(cacheNames = "PermissionById" , key = "T(String).valueOf(#id)")
    @Override
    public Permission getPermissionById(Long id) {
        Permission permission = permissionMapper.selectById(id);
        return permission;
    }
    //添加权限
    @Caching(put = @CachePut(cacheNames = "permission",key = "T(String).valueOf(#permission.id)"),
            evict = {@CacheEvict(cacheNames = "PermissionPage",allEntries = true),
                    @CacheEvict(cacheNames = "Permission",allEntries = true)} )
    @Override
    public String addPermission(Permission permission) {
        int insert = permissionMapper.insert(permission);
        if (insert == 1){
            return "添加成功";
        }else{
            return "添加失败";
        }
    }
    //删除
    @Caching(put = @CachePut(cacheNames = "permission",key = "T(String).valueOf(#permission.id)"),
            evict = {@CacheEvict(cacheNames = "PermissionPage",allEntries = true),
                    @CacheEvict(cacheNames = "Permission",allEntries = true)} )
    @Override
    public String removePermission(Long id) {
        int i = permissionMapper.deleteById(id);
        if (i == 1){
            return "删除成功";
        }else{
            return "删除失败";
        }
    }

    //更新
    @Caching(put = @CachePut(cacheNames = "permission",key = "T(String).valueOf(#permission.id)"),
            evict = {@CacheEvict(cacheNames = "PermissionPage",allEntries = true),
                    @CacheEvict(cacheNames = "Permission",allEntries = true)} )
    @Override
    public String updatePermission(Permission permission) {
        int update = permissionMapper.update(permission, new UpdateWrapper<>(permissionMapper.selectById(permission.getId())));
        if (update == 1){
            return "更新成功";
        }else{
            return "更新失败";
        }
    }
}
